<template>
  <van-form @submit="onSubmit">
    <van-field
        v-model="editUser.currentValue"
        :name="editUser.editKey"
        :label="editUser.editName"
        :placeholder="`用输入 ${editUser.editName}`"
    />
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>
</template>

<script setup lang="ts">
import {useRoute, useRouter} from "vue-router";
import {onMounted, ref} from "vue";
import myAxios from "../request/myAxios";
import {Toast} from "vant";
import {getCurrentUser} from "../states/user";

const route = useRoute();
const router = useRouter()
const editUser = ref({
  editKey: route.query.editKey,
  currentValue: route.query.currentValue,
  editName: route.query.editName,
})
console.log(route.query)
console.log(route.params)
const onSubmit = async () => {
  const currentUser = await getCurrentUser();
  if (!currentUser) {
    Toast.fail('用户未登录');
    return;
  }
  myAxios.put('/user/update', {
    'id': currentUser.id,
    [editUser.value.editKey as string]: editUser.value.currentValue
  }).then(res => {
    if (res.data.code === 0 && res.data.data > 0) {
      Toast.success('修改成功');
      router.back();
    } else {
      Toast.fail('修改失败');
    }
  })
};
</script>

<style scoped>

</style>
